package com.example.smssender;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * 场景列表适配器
 */
public class SceneAdapter extends RecyclerView.Adapter<SceneAdapter.SceneViewHolder> {
    
    private List<SmsContentGenerator.Scene> scenes;
    private OnSceneClickListener listener;
    
    public interface OnSceneClickListener {
        void onSceneClick(SmsContentGenerator.Scene scene);
    }
    
    public SceneAdapter(List<SmsContentGenerator.Scene> scenes, OnSceneClickListener listener) {
        this.scenes = scenes;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public SceneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_scene, parent, false);
        return new SceneViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull SceneViewHolder holder, int position) {
        if (scenes == null || position < 0 || position >= scenes.size()) {
            return;
        }
        
        SmsContentGenerator.Scene scene = scenes.get(position);
        if (scene == null) {
            return;
        }
        
        holder.tvSceneName.setText(scene.getName());
        holder.tvSceneType.setText(scene.getType().getDisplayName());
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null && scene != null) {
                listener.onSceneClick(scene);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return scenes != null ? scenes.size() : 0;
    }
    
    public void updateScenes(List<SmsContentGenerator.Scene> newScenes) {
        this.scenes = newScenes != null ? newScenes : new ArrayList<>();
        notifyDataSetChanged();
    }
    
    static class SceneViewHolder extends RecyclerView.ViewHolder {
        TextView tvSceneName;
        TextView tvSceneType;
        
        SceneViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSceneName = itemView.findViewById(R.id.tvSceneName);
            tvSceneType = itemView.findViewById(R.id.tvSceneType);
        }
    }
}

